package com.ytjj.bigdata.service;

import com.github.pagehelper.PageHelper;
import com.ytjj.bigdata.dao.AdsAppActivityMapper;
import com.ytjj.bigdata.entity.AdsAppActivity;
import com.ytjj.bigdata.model.dto.AdsAppActivityDto;
import com.ytjj.bigdata.model.response.AdsAppActivityResponse;
import com.ytjj.common.api.CommonPageExtend;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Service
@Slf4j
public class AdsAppActivityService {

    @Resource
    private AdsAppActivityMapper adsAppActivityMapper;

    public CommonPageExtend selectAdsAppActivity(AdsAppActivityDto adsAppActivityDto) {
        PageHelper.startPage(adsAppActivityDto.getPageNum(),adsAppActivityDto.getPageSize());
        List<AdsAppActivity> list = adsAppActivityMapper.selectAdsAppActivityAll(adsAppActivityDto);

        if (CollectionUtils.isEmpty(list)) {
            return new CommonPageExtend();
        }

        CommonPageExtend<AdsAppActivity> pageInfo = CommonPageExtend.restPage(list);

        List<AdsAppActivityResponse> adsAppActivityResponses = adsAppActivityMapper.findAdsAppActivity(adsAppActivityDto);
        AdsAppActivityResponse response = adsAppActivityResponses.get(0);
        pageInfo.setPvCount(response.getPvCount());
        pageInfo.setUvCount(response.getUvCount());
        pageInfo.setOrderCountCount(response.getOrderCountCount());
        pageInfo.setPayCountCount(response.getPayCountCount());
        pageInfo.setWxOrderCount(response.getWxOrderCount());
        pageInfo.setAlipayOrderCount(response.getAlipayOrderCount());

         // 1.pv订单率 订单率 -- order_count/pv
        if (response.getPvCount() > 0) {
            BigDecimal b1 = new BigDecimal(response.getOrderCountCount());
            BigDecimal b2 = new BigDecimal(response.getPvCount());
            pageInfo.setOrderPvRateCount(b1.divide(b2, RoundingMode.HALF_UP).doubleValue());
        }
         // 2.uv订单率 订单率 -- order_count/uv
        if (response.getUvCount() > 0) {
            BigDecimal b1 = new BigDecimal(response.getOrderCountCount());
            BigDecimal b2 = new BigDecimal(response.getUvCount());
            pageInfo.setOrderUvRateCount(b1.divide(b2, RoundingMode.HALF_UP).doubleValue());
        }
        // 3.uv支付率 支付率 -- pay_count/uv
        if (response.getUvCount() > 0) {
            BigDecimal b1 = new BigDecimal(response.getPayCountCount());
            BigDecimal b2 = new BigDecimal(response.getUvCount());
            pageInfo.setPayUvRateCount(b1.divide(b2, RoundingMode.HALF_UP).doubleValue());
        }
        // 4.pv支付率 支付率 -- pay_count/pv
        if (response.getPvCount() > 0) {
            BigDecimal b1 = new BigDecimal(response.getPayCountCount());
            BigDecimal b2 = new BigDecimal(response.getPvCount());
            pageInfo.setPayPvRateCount(b1.divide(b2, RoundingMode.HALF_UP).doubleValue());
        }

        return pageInfo;
    }
}
